package in.muton.edushare.components.alertpanel.api;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.components.alertpanel.AlertPanel;
import in.muton.edushare.wicket.models.CayenneModel;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.model.IModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.dao.AssignmentsDAO;
import com.edushare.common.vo.Assignments;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.DateComparision;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public class AlertMgr {
	
	private static final Log log = LogFactory.getLog(AlertPanel.class);
	
	public AlertMgr(Users user) {
		evt_for = EVENT_FOR.USER_EVENTS;
		this.userModel = new CayenneModel<Users>(user);
	}

	public AlertMgr(Departments dept) {
		evt_for = EVENT_FOR.DEPARTMENT_EVENTS;
		this.deptModel = new CayenneModel<Departments>(dept);
	}

	public AlertMgr(Universities univ) {
		evt_for = EVENT_FOR.UNIVERSITY_EVENTS;
		this.univModel = new CayenneModel<Universities>(univ);
	}

	public AlertMgr(CourseInstances course) {
		evt_for = EVENT_FOR.COURSE_EVENTS;
		this.courseModel = new CayenneModel<CourseInstances>(course);
	}

	public List<DateComparision> getEventsforUser() {
		List<DateComparision> snList = new SchAlertMgr(userModel).getScheduleNotifications();
		List<DateComparision> pollList = new PollAlertMgr(userModel).getPolls();
		List<DateComparision> assgnList = new AssgnAlertMgr(userModel).getAssignments();

		List<DateComparision> totalList = new ArrayList<DateComparision>();
		totalList.addAll(snList);
		totalList.addAll(pollList);
		totalList.addAll(assgnList);
		Collections.sort(totalList);
		return totalList;
	}

	public List<DateComparision> getEvents() {
		if (evt_for == EVENT_FOR.USER_EVENTS)
			return getEventsforUser();
		if (evt_for == EVENT_FOR.COURSE_EVENTS)
			return new ArrayList<DateComparision>();
		if (evt_for == EVENT_FOR.UNIVERSITY_EVENTS)
			return new ArrayList<DateComparision>();
		if (evt_for == EVENT_FOR.DEPARTMENT_EVENTS)
			return new ArrayList<DateComparision>();
		return new ArrayList<DateComparision>();
	}

	public static List<DateComparision> getEventsByGroup(IModel<?> obj,
			int creatorType, int creatorId) {
		List<DateComparision> sList = new SchAlertMgr(obj).getSchedules(false);
		log.debug("Slist size = "+ sList.size());
		List<DateComparision> pollList = PollAlertMgr.getPolls(creatorType,
				creatorId);
		log.debug("Polllist size = "+ pollList.size());
		List<DateComparision> dList = new ArrayList<DateComparision>();
		if (obj.getObject() instanceof CourseInstances) {
			List<Assignments> asList = 
				new AssignmentsDAO(EduShareSession.get()
					.getSessionORMInterface()).getAssignments(
								((CourseInstances) obj.getObject()).getCourseInstanceId(),
								ApplicationConstants.PAGE_SIZE,
								Assignments.LATE_SUBM_DATE_PROPERTY,
								ApplicationConstants.SORT_ORDER_DESCENDING
							);

			for (Assignments as : asList) {
				if (as.getDate().getTime() > new Date().getTime()) {
					dList.add(as);
				}
			}
			Collections.sort(dList);
		}
		log.debug("dlist size = "+ dList.size());
		sList.addAll(pollList);
		sList.addAll(dList);
		Collections.sort(sList);
		return sList;
	}

	private enum EVENT_FOR {
		UNIVERSITY_EVENTS, DEPARTMENT_EVENTS, COURSE_EVENTS, USER_EVENTS
	};

	private IModel<Users> userModel;
	private IModel<Departments> deptModel;
	private IModel<Universities> univModel;
	private IModel<CourseInstances> courseModel;
	private EVENT_FOR evt_for;
}
